package com.course.redisson.service;

import com.course.model.dto.UserDto;
import com.course.model.entity.SysLog;
import com.course.model.mapper.SysLogMapper;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @PROJECT_NAME: course
 * @DESCRIPTION: 记录用户登录日志
 * @Author: 涂玄武
 * @DATE: 2021/5/31 15:36
 */
@Service
public class SysLogService {

    private static final Logger logger = LoggerFactory.getLogger(SysLogService.class);

    @Resource
    private SysLogMapper sysLogMapper;

    //解决Gson转换json对象字段为null时转换问题
    private static Gson GSON = new GsonBuilder().serializeNulls().create();

    /**
     * 记录用户登录成功后的轨迹
     * @param userDto
     */
    public void recordLog(UserDto userDto){
        SysLog sysLog = new SysLog();
        sysLog.setUserId(Integer.parseInt(userDto.getId()));
        sysLog.setCreateAt(new Date());
        sysLog.setData(GSON.toJson(userDto));
        sysLog.setModule("用户登录");
        sysLogMapper.insertSelective(sysLog);
    }

}
